package com.qqt.csr.file.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.obs.services.model.*;
import com.qqt.csr.common.enums.FileGroupEnum;
import com.qqt.csr.common.enums.FileTypeEnum;
import com.qqt.csr.common.exception.StatusCode;
import com.qqt.csr.common.utils.ServiceAssert;
import com.qqt.csr.file.dto.DownloadFileDTO;
import com.qqt.csr.file.service.IFileFolderService;
import com.qqt.csr.file.service.ISingleFileService;
import com.qqt.csr.file.vo.resp.FileUploadInfoResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SingleFileHWOBSServiceImpl extends AbstractHWOBSService implements ISingleFileService {
    @Autowired
    private IFileFolderService fileFolderService;

    @Override
    public FileUploadInfoResp uploadFile(String bucketName, String fileName, FileTypeEnum fileType, InputStream file) {
        fileName = FileUtil.getName(fileName);
        String fileFolder = fileFolderService.getFileFolder(bucketName, fileType);
        String objectKey = fileFolder + fileName;

        PutObjectRequest request = new PutObjectRequest();
        request.setBucketName(bucketName);
        request.setObjectKey(objectKey);
        request.setInput(file);
        // 默认对象公共读
        request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(fileType.getContentType());
        if (FileGroupEnum.IMG.equals(fileType.getGroup())) {
            objectMetadata.setContentDisposition(String.format("inline;attachment;filename=\"%s\"", fileName));
        } else {
            objectMetadata.setContentDisposition(String.format("attachment;filename=\"%s\"", fileName));
        }
        objectMetadata.setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS).mustRevalidate().getHeaderValue());
        objectMetadata.setObjectStorageClass(StorageClassEnum.STANDARD);
        request.setMetadata(objectMetadata);

        PutObjectResult result = obsClient.putObject(request);
        ServiceAssert.isTrue(isRequestSuccess(result), StatusCode.Common.THIRD_API_ERROR.getCode(), "文件上传失败");
        String url = domainUrl + URLUtil.getPath(result.getObjectUrl());
        return FileUploadInfoResp.builder().url(url).build();
    }

    /**
     * 下载文件
     * @param bucketName
     * @param objectKey
     * @return
     */
    public DownloadFileDTO downloadFile(String bucketName, String objectKey) {
        GetObjectRequest request = new GetObjectRequest(bucketName, objectKey);
        ObsObject obsObject = obsClient.getObject(request);
        return DownloadFileDTO.builder()
                .fileName(obsObject.getObjectKey())
                .inputStream(obsObject.getObjectContent())
                .build();
    }
}
